{% load static %}
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="{% static 'css/bootstrap-3.3.7-dist/css/bootstrap.min.css' %}">
    <script src="{% static 'js/jquery.js' %}"></script>
    <style>
        .code {
            width: 350px;
            height: 35px;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <h1 class="text-center">登录</h1>
            <div class="form-group">
                <label for="username">用户名:</label>
                <input type="text" id="username" name="username" class="form-control">
            </div>
            <div class="form-group">
                <label for="password">密码:</label>
                <input type="password" id="password" name="password" class="form-control">
            </div>
            <div class="form-group">
                <label for="id_code">验证码:</label>
                <div class="row">
                    <div class="col-md-6">
                        <input type="text" name="code" id="id_code" class="form-control">
                    </div>
                    <div class="col-md-6">
                        <img src="{% url 'gc' %}" alt="" class="code">
                    </div>
                </div>
            </div>
            <input type="button" class="btn btn-success" value="登录" id="id_commit">
            <span style="color: red;" id="error"></span>
        </div>
    </div>
</div>
<script>
    $('.code').click(function() {
        $(this).attr('src', '{% url 'gc'  %}' + '?ran=' + Math.random());
    });

    $('#id_commit').click(function() {
       $.ajax({
           url: '',
           type: 'post',
           data: {
               'username': $('#username').val(),
               'password': $('#password').val(),
               'id_code': $('#id_code').val(),
               'csrfmiddlewaretoken': '{{ csrf_token }}'
           },
           success:function(args) {
                if(args.code === 1000) {
                    window.location.href = args.url;
                } else {
                    // 渲染错误信息
                    $('#error').text(args.msg);
                }
           }
       }) ;
    });
</script>
</body>
</html>